package com.example.jh352160.surfingtv.thematic_details;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.network.item.ThematicDetailItem;
import com.example.jh352160.surfingtv.network.retrofit.CommonRetrofit;
import com.example.jh352160.surfingtv.network.service.ThematicDetailService;
import com.example.jh352160.surfingtv.thematic_details.customView.AllCommodity;
import com.example.jh352160.surfingtv.thematic_details.customView.Favorite_button;
import com.example.jh352160.surfingtv.thematic_details.customView.Share_page;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import static android.view.View.VISIBLE;

/**
 * Created by jh352160 on 2016/7/28
 */

public class ThematicDetailsPage extends AppCompatActivity {

    private static final int NETWORK_OK = 0;
    Share_page sharePage;
    Toolbar toolbar;
    WebView webView;
    TextView titleView;
    Favorite_button favoriteButton;
    String detailUrl, title;
    Boolean isFavorite;
    int favoriteCount;

    int contentId;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case NETWORK_OK:
                    init();
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.thematic_details_page);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        titleView = (TextView) toolbar.findViewById(R.id.thematic_title);
        webView = (WebView) findViewById(R.id.webView);
        favoriteButton = (Favorite_button) findViewById(R.id.favority);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("");
        ImageView shareButton = (ImageView) findViewById(R.id.share_button);
        ImageView allCommodity = (ImageView) findViewById(R.id.thematic_all_commodity);
        sharePage = (Share_page) findViewById(R.id.share_page);
        shareButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sharePage.setVisibility(VISIBLE);
                sharePage.startAnim();
            }
        });

        allCommodity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ThematicDetailsPage.this, AllCommodity.class);
                intent.putExtra("contentId", contentId);
                startActivity(intent);
            }
        });

        Intent intent = getIntent();
//        webView.loadUrl(intent.getStringExtra("detailUrl"));
//        titleView.setText(intent.getStringExtra("title"));
        contentId = intent.getIntExtra("contentId", 0);
//        favoriteButton.setData(intent.getBooleanExtra("isfavorite",false),intent.getIntExtra("favoriteCount",0),intent.getIntExtra("contentId",0));

        //api
        Retrofit retrofit = CommonRetrofit.getRetrofit();
        ThematicDetailService thematicDetailService = retrofit.create(ThematicDetailService.class);
        Call<ThematicDetailItem> call = thematicDetailService.getItem(contentId);
        call.enqueue(new Callback<ThematicDetailItem>() {
            @Override
            public void onResponse(Call<ThematicDetailItem> call, Response<ThematicDetailItem> response) {
                detailUrl = response.body().getData().getDetailUrl();
                title = response.body().getData().getTitle();
                isFavorite = response.body().getData().getFavorite() == 1;
                favoriteCount = response.body().getData().getFavoriteCount();
                Message message = new Message();
                message.what = NETWORK_OK;
                handler.sendMessage(message);
            }

            @Override
            public void onFailure(Call<ThematicDetailItem> call, Throwable t) {

            }
        });
    }

    private void init() {
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(detailUrl);
        titleView.setText(title);
        favoriteButton.setData(isFavorite, favoriteCount, contentId);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
